
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Pool;

public class UI_BuffItem : MonoBehaviour
{
    [SerializeField, Header("遮罩层")]
    private Image m_Mask_M;
    [SerializeField, Header("等级文本")]
    private TextMeshProUGUI m_Level;
    
    [SerializeField, Header("边框")]
    private Image m_Frame;
    [SerializeField, Header("图标")]
    private Image m_Icon;

    [Space]
    [Header("Buff详情")]
    [SerializeField, Header("详情弹窗")]
    private GameObject m_BuffInfo;
    [SerializeField, Header("Buff名称文本")]
    private TextMeshProUGUI m_BuffName;

    [SerializeField, Header("Buff描述文本")]
    private TextMeshProUGUI m_Description;

    [SerializeField, Header("Buff来源文本")]
    private TextMeshProUGUI m_Provider;


    private ObjectPool<UI_BuffItem> m_RecyclePool;


    private bool m_Initialized = false;// 是否已经初始化
    private bool m_NeedNumber = false;// 是否需要显示等级
    private bool m_NeedLine = false;// 是否需要显示计时工具


    private BuffBase m_TargetBuff;

    public void OnPointerEnter()
    {
        m_BuffInfo.gameObject.SetActive(true);
        ShowInfo(m_TargetBuff);
    }

    // 显示Buff详细信息
    public void ShowInfo(BuffBase buff)
    {
        m_BuffName.text = buff.Name;
        m_Description.text = buff.Description;
        m_Provider.text = "来自：" + buff.Provider;
    }

    public void OnPointerExit()
    {
        m_BuffInfo.gameObject.SetActive(false);
    }
    public void Initialize(BuffBase buff, ObjectPool<UI_BuffItem> recyclePool)
    {
        m_Icon.sprite = Resources.Load<Sprite>(buff.IconPath);
        m_TargetBuff = buff;
        m_RecyclePool = recyclePool;
        if (m_TargetBuff.MaxLevel > 1)
        {
            m_NeedNumber = true;
            m_Level.gameObject.SetActive(true);
        }
        else
        {
            m_NeedNumber = false;
            m_Level.gameObject.SetActive(false);
        }


        if (m_TargetBuff.TimeScale > 0)
        {
            m_NeedLine = true;
            m_Mask_M.gameObject.SetActive(true);
        }
        else
        {
            m_NeedLine = false;
            m_Mask_M.gameObject.SetActive(false);
        }

        switch (buff.BuffType)
        {
            case BuffType.Buff:
                m_Frame.color = Color.green;
                break;
            case BuffType.Debuff:
                m_Frame.color = Color.red;
                break;
            case BuffType.None:
                m_Frame.color = Color.white;
                break;
            default:
                break;
        }
        m_Initialized = true;
    }

    private void Update()
    {
        if (m_Initialized)
        {
            //需要显示计时工具才显示
            if (m_NeedLine)
            {
                m_Mask_M.fillAmount = 1 - (m_TargetBuff.ResidualDuration / m_TargetBuff.MaxDuration);
            }
            //需要显示等级才显示
            if (m_NeedNumber)
            {
                m_Level.text = m_TargetBuff.CurrentLevel.ToString();
            }
            //如果当前等级等于零说明他已经被废弃了，所以就可以回收了
            if (m_TargetBuff.CurrentLevel == 0 )
            {
                m_RecyclePool.Release(this);
            }
        }
    }
}
